package org.example.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MybatisUtils {
    //在每个sqlsession中维护了一个connection对象
    public static SqlSession getSqlSession()  {
        //创建ssfbuilder对象
        SqlSessionFactoryBuilder ssf = new SqlSessionFactoryBuilder();
        //传入主配置文件文件流
        InputStream in = MybatisUtils.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
        //InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory build = ssf.build(in);
        SqlSession sqlSession = build.openSession(true);

        return sqlSession;
    }
}
